<?php
class User_management extends CI_Controller {
	
   	function __construct()
	{
		parent::__construct();
		$this->functions->checkAuthAdmin($this->controller.'/');
		$this->load->model('ModelUser_management');
		$this->controller = 'user_management';
	}
	
	function index(){
		
		$this->functions->checkAuthAdmin($this->controller.'/');
		
		$id = $this->uri->segment(3, 0);
		$page = $this->uri->segment(4, 0); 
		if($id){
		$editDetails = $this->ModelUser_management->AddAdminUseredit($id);
		
		$data['editDetails'] = $editDetails;
		$data['value']  = lang('update');		
		$data['addlink'] = base_url($this->controller."/do_addedit/".$id."/".$page."/");
			
		}else{
		$data['addlink'] = base_url($this->controller."/adduser/");;	
		$data['value']  = lang('Add');		
		}
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'user/adduser';

		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
		
	}
	
	function adduser(){
		
		$this->functions->checkAuthAdmin($this->controller.'/adduser/');
		
		$this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique['.ADMIN.'.username]|min_length[5]|max_length[20]');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[5]|max_length[20]|matches[confirm_password]|xss_clean');
		$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[5]|max_length[20]|xss_clean');
		$this->form_validation->set_rules('email_address', 'Email Address','trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('full_name','Full Name','trim|required|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	
		$id = $this->nsession->userdata('user_session_id');
		$data['username'] = $this->input->request('username');
		$data['password'] = md5($this->input->request('password'));
		$data['email_address'] = $this->input->request('email_address');
		$data['full_name'] = $this->input->request('full_name');
		$data['role'] = $this->input->request('role');
		$data['status'] = $this->input->request('status');
		$data['date_added'] = date('Y-m-d');
		if($this->form_validation->run() == TRUE)
		{	
			
			
			$isTrue = $this->ModelUser_management->AddAdminUser($data);
	
			if($isTrue){
				$this->viewuser();
				//echo 'hii'; die;
				/*$data['new_admin_pwd'] = $this->input->request('new_admin_pwd');
				$this->ModelLogin->updateAdminPass($id,$data);*/
				$this->nsession->set_userdata('succmsg',"Added Successfully");
				
			}else{
				$this->nsession->set_userdata('errmsg',"Error ...");
			}
	        redirect(base_url($this->controller."/viewuser/"));
			return true;
		}else{
			
			$value[] = $data;
			$data['editDetails']=$value;
			$data['addlink'] = base_url($this->controller."/adduser/");;	
			$data['value']  = lang('Add');	
			$this->nsession->set_userdata('succmsg', "");
			$this->nsession->set_userdata('errmsg', "");
			
			$elements = array();
			$elements['menu'] = 'menu/index';
			$elements['main'] = 'user/adduser';
	
			$element_data['menu'] = array();
			$element_data['main'] = $data;
			
			$this->layout->setLayout('layout_main_view'); 
			$this->layout->multiple_view($elements,$element_data);
		}
	}
	
	function viewuser(){
		$this->functions->checkAuthAdmin($this->controller.'/viewuser/');
		/*print_r($_SESSION);
		die;*/
		$page = $this->uri->segment('3');
		$config['base_url'] 			= base_url($this->controller."/viewuser/");

		$config['uri_segment']  		= 3;
		$config['num_links'] 			= 10;
		$config['page_query_string'] 		= false;
		$config['extra_params'] 		= "";
		$config['extra_params'] 		= "";

		$this->pagination->setAdminPaginationStyle($config);
		$start = 0;

		$data['controller'] = $this->controller;
		
		$param['sortType'] 		= $this->input->request('sortType','DESC');
		$param['sortField'] 		= $this->input->request('sortField','date_added');
		$param['searchField'] 		= $this->input->request('searchField','');
		$param['searchText'] 		= $this->input->request('searchText','');
		$param['searchFromDate'] 	= $this->input->request('searchFromDate','');
		$param['searchToDate'] 		= $this->input->request('searchToDate','');
		$param['searchDisplay'] 	= $this->input->request('searchDisplay','10');/**/
		

		$data['recordset'] 			= $this->ModelUser_management->getList($config,$start,$param);
		
		
		$data['startRecord'] 		= $start;
		
		$this->pagination->initialize($config);
       
		$data['params'] 			= $this->nsession->userdata('ADMIN_USER');
		$data['search_link']      	= base_url($this->controller."/viewuser/0/1/");
		$data['reload_link']      	= current_url();
		$data['add_link']         	= base_url($this->controller."/adduser/0/0/");
		$data['edit_link']        	= base_url($this->controller."/index/{{ID}}/".$page."");
		$data['delete_link']      	= base_url($this->controller."/delete/{{ID}}/0");
		$data['active_link']      	= base_url($this->controller."/changestatus/{{ID}}/".$page."");
		$data['detail_link']      	= base_url($this->controller."/view_detail/{{ID}}/");
		$data['showall_link']     	= base_url($this->controller."/viewuser/0/1");
		$data['active_all_link']    = $this->controller."/activeall/";
		$data['inactive_all_link']  = $this->controller."/inactiveall/";
		$data['password_link']		= base_url($this->controller."/changepassword/{{ID}}/");
		$data['total_rows']			= $config['total_rows'];

		$data['download_link']      = base_url($this->controller."/download/");

		$data['succmsg'] 	= $this->nsession->userdata('succmsg');
		$data['errmsg'] 	= $this->nsession->userdata('errmsg');
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");

		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'user/viewuser';
		$element_data['menu'] = array();
		$element_data['main'] = $data;

		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);
	}
	
	function do_addedit(){
		$this->functions->checkAuthAdmin($this->controller.'/do_addedit/');
		$id = $this->uri->segment(3, 0);
		$this->nsession->set_userdata('do_addedit_id',$id);
		$page = $this->uri->segment(4, 0);
		$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[20]|callback_username_check');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[5]|max_length[20]|matches[confirm_password]|xss_clean');
		$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[5]|max_length[20]|xss_clean');
		$this->form_validation->set_rules('email_address', 'Email Address','trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('full_name','Full Name','trim|required|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	
		
		//$data['msg'] = "";
		//echo 'aaa'; die;
		if($this->form_validation->run() == TRUE)
		{	
			$data['username'] = $this->input->request('username');
			$data['password'] = md5($this->input->request('password'));
			$data['email_address'] = $this->input->request('email_address');
			$data['full_name'] = $this->input->request('full_name');
			$data['status'] = $this->input->request('status');
			$data['date_added'] = date('Y-m-d');
			
			$isTrue = $this->ModelUser_management->UpdateAdminUser($data,$id);
			if($isTrue){
				$this->nsession->set_userdata('succmsg',"Updated Successfully");
			}else{
				$this->nsession->set_userdata('errmsg',"Updated Failed");	
			}
			redirect(base_url($this->controller."/viewuser/".$page));
			return true;
		}else{
			
			$editDetails = $this->ModelUser_management->AddAdminUseredit($id);
			$data['editDetails'] = $editDetails;
			$data['value']  = lang('edit');		
			$data['addlink'] = base_url($this->controller."/do_addedit/".$id."/".$page."/");
			
			$this->nsession->set_userdata('succmsg', "");
			$this->nsession->set_userdata('errmsg', "");
			
			$elements = array();
			$elements['menu'] = 'menu/index';
			$elements['main'] = 'user/adduser';
			
			$element_data['menu'] = array();
			$element_data['main'] = $data;
			
			$this->layout->setLayout('layout_main_view'); 
			$this->layout->multiple_view($elements,$element_data);
			
		}
	}
	
	public function username_check($str)
	{
		$do_addedit_id = $this->nsession->userdata('do_addedit_id');
		$result = $this->ModelUser_management->UpdateUsername_check($str,$do_addedit_id);
		
		if ($result >'0')
		{
			$this->nsession->unset_userdata('do_addedit_id');
			$this->form_validation->set_message('username_check', 'The %s field must contain a unique value.');
			return FALSE;
		}
		else
		{
			$this->nsession->unset_userdata('do_addedit_id');
			return TRUE;
		}
	}
	
	function delete()
	{
		$this->functions->checkAuthAdmin($this->controller.'/delete/');
		$id = $this->uri->segment(3, 0);
		$data['id'] = $id;
		$this->ModelUser_management->delete($data);
		$this->nsession->set_userdata('succmsg', 'Successfully Admin User Deleted.');
		redirect(base_url($this->controller."/viewuser/"));
		return true;
	}
	
	function changestatus()
	{
		$this->functions->checkAuthAdmin($this->controller.'/changestatus/');

		$page = $this->uri->segment(4, 0);
		$id = $this->uri->segment(3, 0);
		$data['errmsg'] = "";
		$this->ModelUser_management->changeStatus($id);
		$this->nsession->set_userdata('succmsg','Successfully Admin User Status Changed.');
		redirect(base_url($this->controller."/viewuser/".$page));
		return true;
	}
	
	function inactiveall()
	{
		$this->functions->checkAuthAdmin($this->controller.'/inactiveall/');

		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->ModelUser_management->changeStatus($id,'inactive');
		}
		$this->nsession->set_userdata('succmsg', 'Successfully Admin User Status Changed.');
		redirect(base_url($this->controller."/viewuser/"));
		return true;
	}
	
	function activeall()
	{
		$this->functions->checkAuthAdmin($this->controller.'/activeall/');

		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->ModelUser_management->changeStatus($id,'active');
		}
		$this->nsession->set_userdata('succmsg', 'Successfully Admin User Status Changed.');
		redirect(base_url($this->controller."/viewuser/"));
		return true;
	}
	
	function deleteall()
	{
		$this->functions->checkAuthAdmin($this->controller.'/');
		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->ModelUser_management->deleteALL($id);
		}
		$this->nsession->set_userdata('succmsg', 'Successfully Admin User Deleted.');
		redirect(base_url($this->controller."/viewuser/"));
		return true;
	}
}